$(function() {
	admin.func.init();
});
var templates,tType=-1;
Namespace('admin.func',{
	init:function() {
		admin.func.netMsgChange('msgText',60);
	},
	setMsg:function(value){
		tType = value;
		if(value==-1){
			$("#msgText").val("");
			$("#p_tip").show();
			$("#msgText").removeAttr("readonly");
		}else{
			$("#p_tip").hide();
			$("#msgText").val(templates[value].content);
			$("#msgText").attr("readonly","readonly");
		}
	},
	toSendSms:function(){
		var checkedItems = $('#datagrid').datagrid('getChecked');
		$('#templateList li').each(function(){
		    $(this).remove();
		}); 
		$('#userList li').each(function(){
		    $(this).remove();
		});
		if(checkedItems.length>30){
			top.showInfo('一次最多给30个用户发送短信!');
		}
		if(checkedItems.length>0){
			$.post(contextPath + '/common/sms/getTemplate', {}, function (result) {
				templates = result.map.templates;
	            if (result.code == 0) {
	            	$("#templateList").append('<li ><input type="radio" onclick="admin.func.setMsg(-1);" checked="checked" value="-1" name="templateType"/>自定义</li>');
	            	for (var i in templates) {
	            		$("#templateList").append('<li ><input type="radio" onclick="admin.func.setMsg('+i+');" value="'+i+'" name="templateType"/>'+templates[i].name+'</li>');
					}
	            	$('#w').window('open');
	    			$.each(checkedItems, function(index, item){
	    				$("#userList").append("<li>"+item.userName+"</li>");
	    			});
	            } else {
	                top.showInfo("获取模板失败："+result.msg);
	            }
	        });
		}else{
			top.showInfo('请先选择用户!');
		}
	},
	doSendSms:function(){
		var msg = $("#msgText").val();
		if(!msg){
			top.showInfo('短信内容不能为空!');
			return;
		}
		if(msg.length>60){
			top.showInfo('短信内容太长!');
			return;
		}
		if(msg.indexOf("{userName}")){
			msg = msg.replace(/{userName}/g, "\{userName\}");
		}
		var array = new Array();
		var checkedItems = $('#datagrid').datagrid('getChecked');
		$.each(checkedItems, function(index, item){
			array.push(item.id);
		});
		var tId = -1;
		if(tType!=-1){
			tId=templates[tType].id;
		}
		$.post(contextPath + '/common/sms/doSendSms', {tId:tId,msg:msg,userList:array}, function (result) {
            if (result.code == 0) {
            	$('#w').window('close');
                top.showInfo('短信发送完毕!');
            } else {
                top.showInfo("短信发送失败："+result.msg);
            }
        });
	},
	netMsgChange:function(id,maxCount) {
		var inputLength = $('#'+id).val().length;
		if (inputLength == 0) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + maxCount + '个字');
			saveFlag = false;
		} else if (inputLength > 0 && inputLength <= maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + (maxCount - inputLength) + '个字');
			saveFlag = false;
		} else if (inputLength > maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，已经输入超过<strong class="cred">' + (inputLength - maxCount) + '</strong>个字');
			saveFlag = true;
		}
	}
});